package Client

import (
	"fmt"
	"net/rpc"
)

func TcpClient() {
	rpc, err := rpc.Dial("tcp", "127.0.0.1:10020")
	if err != nil {
		fmt.Println("client tcp err :", err)
		return
	}
	r := &RpcTestStruct{A: 19, Name: "a", Ctx: "qqqqq"}
	var reply int
	err = rpc.Call("SelfType.SayHello", r, &reply)
	if err != nil {
		fmt.Println("r err :", err)
	}
	fmt.Println("Call :", reply)

	fmt.Println("---------------------")
	temp := &MethodsType{}
	err = rpc.Call("SelfType.TellMethod", &r, &temp)
	if err != nil {
		fmt.Println("!!!!!!!!!!!!!!!!!")
		fmt.Println("r err ...:", err)
	}
	fmt.Println("---------------------")
	fmt.Println(temp)
	//temp.TestFunc(string(temp.B))
	fmt.Println(temp.SType.Name)
}

func clientHttp() {
	client, err := rpc.DialHTTP("tcp", "127.0.0.1:1998")
	if err != nil {
		fmt.Println("client err occ :", err)
		return
	}
	r := &RpcTestStruct{A: 19, Name: "a", Ctx: "qqqqq"}
	var reply int
	err = client.Call("SelfType.SayHello", r, &reply)
	if err != nil {
		fmt.Println("r err :", err)
	}
	fmt.Println("Call :", reply)

	var reply1 int
	r1 := &RpcTestStruct{A: 22, Name: "b", Ctx: "wwwwww"}
	divCall := client.Go("SelfType.SayHello", r1, &reply1, nil)
	replyCode := <-divCall.Done
	if replyCode.Error != nil {
		fmt.Println(" Go occ :", replyCode.Error)
	}
	fmt.Println(" Go :", reply1)
}

type RpcTestStruct struct {
	A    int
	Name string
	Ctx  string
}

type MethodsType struct {
	A        int
	TestFunc func(string)
	B        string
	SType    *SelfType
	TestMap  map[int]interface{}
}

type SelfType struct {
	Name string
}
